package cn.stylefeng.guns.modular.classification.controller;

import cn.stylefeng.roses.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import cn.stylefeng.guns.modular.system.model.Classification;
import cn.stylefeng.guns.modular.classification.service.IClassificationService;

/**
 * 分类表控制器
 *
 * @author fengshuonan
 * @Date 2021-04-10 19:40:28
 */
@Controller
@RequestMapping("/classification")
public class ClassificationController extends BaseController {

    private String PREFIX = "/classification/classification/";

    @Autowired
    private IClassificationService classificationService;

    /**
     * 跳转到分类表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "classification.html";
    }

    /**
     * 跳转到添加分类表
     */
    @RequestMapping("/classification_add")
    public String classificationAdd() {
        return PREFIX + "classification_add.html";
    }

    /**
     * 跳转到修改分类表
     */
    @RequestMapping("/classification_update/{classificationId}")
    public String classificationUpdate(@PathVariable Integer classificationId, Model model) {
        Classification classification = classificationService.selectById(classificationId);
        model.addAttribute("item",classification);
        LogObjectHolder.me().set(classification);
        return PREFIX + "classification_edit.html";
    }

    /**
     * 获取分类表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return classificationService.selectList(null);
    }

    /**
     * 新增分类表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Classification classification) {
        classificationService.insert(classification);
        return SUCCESS_TIP;
    }

    /**
     * 删除分类表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer classificationId) {
        classificationService.deleteById(classificationId);
        return SUCCESS_TIP;
    }

    /**
     * 修改分类表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Classification classification) {
        classificationService.updateById(classification);
        return SUCCESS_TIP;
    }

    /**
     * 分类表详情
     */
    @RequestMapping(value = "/detail/{classificationId}")
    @ResponseBody
    public Object detail(@PathVariable("classificationId") Integer classificationId) {
        return classificationService.selectById(classificationId);
    }
}
